﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.QuanTri
{
    public partial class QLThoiKhoaBieu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Pager2.CurrentIndex = 1;
                Bind_Data1(Pager2.CurrentIndex);
            }
        }
        protected void Bind_Data1(int PageNo)
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            if (txtTuKhoa.Text != "")
            {
                WhereClause += " and MaLopHP like '%" + txtTuKhoa.Text + "%'"; 
            }
            DataSet dsThongBao = DataRepository.Provider.ExecuteDataSet("ThoiKhoaBieu_GetPaged", WhereClause, "MaTKB DESC", PageNo - 1, Pager2.PageSize);
            grvChuyenMuc.DataSource = dsThongBao.Tables[0];
            grvChuyenMuc.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsThongBao.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager2.ItemCount = TotalRows;
            if (TotalRows <= Pager2.PageSize)
                Pager2.Visible = false;
            else
                Pager2.Visible = true;
        }

        protected void Pager2_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager2.CurrentIndex = currnetPageIndex;
            Bind_Data1(currnetPageIndex);
        }

        protected void gridCatalog2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int IdTKB = Convert.ToInt32(e.CommandArgument.ToString());
                Response.Redirect("~/DCTEDU_/QuanTri/CNThoiKhoaBieu.aspx?Id=" + IdTKB);
            }

            if (e.CommandName == "Delete")
            {
                try
                {
                    int IdTKB = Convert.ToInt32(e.CommandArgument.ToString());
                    bool deleteStatus = DataRepository.ThoiKhoaBieuProvider.Delete(IdTKB);
                    if (!deleteStatus)
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.XOA_THATBAI, true);
                    else
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.XOA_THANHCONG, true);
                    Bind_Data1(Pager2.CurrentIndex);
                }
                catch (Exception ex)
                {
                    Common.WriteLog("QLThoiKhoaBieu.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.XOA_THATBAI, true);
                }
            }
        }
        protected void gridCatalog2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Bind_Data1(Pager2.CurrentIndex);
        }

        protected void cmdTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                Pager2.CurrentIndex = 1;
                Bind_Data1(Pager2.CurrentIndex);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DCTEDU_/QuanTri/QLThoiKhoaBieu.aspx.cs", "cmdTimKiem_Click", ex.ToString());
            }
        }

        protected void gridCatalog2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                    e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("DCTEDU_/QuanTri/QLThoiKhoaBieu.aspx.cs", "gridCatalog_RowDataBound", ex.ToString());
            }
        }

        public string GetTenPhong(object ob)
        {
            string id = ob.ToString();
            if (id != "")
            {
                PhongHoc ent_PhongHoc = DataRepository.PhongHocProvider.GetByMaPhong(Convert.ToInt32(id));
                if (ent_PhongHoc != null)
                {
                    return ent_PhongHoc.TenPhong;
                }
            }
            return "";
        }
    }
}